package com.czxy.dem01;

import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Properties;

public class Test27 {

    public static void main(String[] args) throws DocumentException, InvocationTargetException, IllegalAccessException {

        SAXReader s=new SAXReader();
        Document read = s.read(new File("beans.xml"));
        Element rootElement = read.getRootElement();
        List<Element> elements = rootElement.elements();
        Properties car=new Properties();
        Properties book=new Properties();
        for (Element element : elements) {
            String classname = element.attributeValue("className");

            if (classname.equals("cn.itcast.domain.Car")){
                List<Element> elements1 = element.elements();
                for (Element element1 : elements1) {
                    String name = element1.attributeValue("name");
                    String value = element1.attributeValue("value");
                    BeanUtils.setProperty(car,name,value);
                }

            }else {
                List<Element> elements1 = element.elements();
                for (Element element1 : elements1) {
                    String name = element1.attributeValue("name");
                    String value = element1.attributeValue("value");
                    BeanUtils.setProperty(book,name,value);
                }
            }
        }
        System.out.println(car);
        System.out.println(book);

    }
}
